import xhr from "@/utils/http";
import {getQueryVariable} from '@/utils/getQueryParam'
var coordtransform=require('coordtransform');
var gcj02towgs84=coordtransform.gcj02towgs84;
export const addPolygon = (data = {
  area: '',
  config: ''
}) => {
  return xhr.request({
    url: '/facNcjlMapping',
    method: 'post',
    data,
  })
}
export const getPolygonList = () => {
  return xhr.request({
    url: '/facNcjlMapping/queryList',
    method: 'get',
  })
}
export const updateSite = (data) => {
  return xhr.request({
    url: '/facNcjlMapping/edit',
    method: 'post',
    data,
  })
}

export const getSiteList = () => {
  return xhr.request({
    // url: '/ncjl/land/getSelectListNoPage',
    url: '/ncjl/land/queryListForaMapping',
    method: 'get',
  })
}

export const getPolygonById = (id) => {
  return xhr.request({
    url: `/facNcjlMapping/queryById?id=${id}`,
    method: 'get',
  })
}

export const getFamringRecord = (landId) => {
  return xhr.request({
    url: `ncjl/farming/queryById?landId=${landId}`,
    method: 'get',
  })
}

export const getJsapiTicket = (isFlash = 1) => {
  return xhr.get(`/sys/wechat/jsapi?isFlash=${isFlash}`)
}

export const delPolygon = (ids = []) => {
  return xhr.request({
    url: `/facNcjlMapping/delIds`,
    method: 'post',
    data: ids
  })
}


let pointList = [
  [104.10015574629415,30.400384590526897],[104.10019866163793,30.400382277094096],[104.10015038187618,30.40037765022825],[104.10020939047388,30.400373023362206],[104.10015038187618,30.400370709929092],[104.1002040260559,30.400368396495935],[104.10014233524839,30.40034988902869],[104.10019866163793,30.40034988902869],[104.10012624199447,30.40037765022825],[104.10021743709996,30.4004077248522],[104.1001450174565,30.400414665149484],[104.10098454887856,30.400696903459675],[104.10219958956208,30.4005904858316],[104.10269870468932,30.398924658545056],[104.10123029350757,30.396666075801406],[104.09981251719461,30.396884449341655],[104.09883598759126,30.39816348456057],[104.10015574629415,30.400384590526897]
];
let index = pointList.length
const mock = () => {
  const [longitude, latitude] = pointList[++index % pointList.length]
  return {
    longitude, latitude, accuracy: 8
  }
}
export const getLocation = async () => {
  const {data} = await xhr.request({
    url: `/sys/wechat/h5Msg`,
    method: 'post',
    data: {
      type: 'receive'
    },
  });
  if (!data) return 
  
  const [longitude, latitude] = gcj02towgs84 (data.longitude, data.latitude)
  return {
    ...data,
    longitude, latitude
  }
  // return mock ();
}


export const getDeviceList = async () => {
  const params = getQueryVariable ();
  const tenantId = params.tenantId;
  return await xhr.request({
    // url: `/device/DeviceManage/list`,
    url: `/sys/tenant/portalDeviceList`,
    method: 'post',
    data: {
      pageNum: 1, 
      pageSize: 1000
    },
  });
}

export const getInsectInfo = async (deviceId) => {
  return await xhr.request({
    url: `/ncjl/monitor/getInsectImage?deviceId=${deviceId}`,
    method: 'get',
  });
}
export const getSoilInfo = async (deviceId) => {
  return await xhr.request({
    url: `/ncjl/monitor/getSoilInfo?deviceId=${deviceId}`,
    method: 'get',
  });
}
export const getWeatherInfo = async (deviceId) => {
  return await xhr.request({
    url: `/ncjl/monitor/getWeatherInfo?deviceId=${deviceId}`,
    method: 'get',
  });
}

export const getDeviceVideo = async (deviceId) => {
  const params = getQueryVariable ();
  const tenantId = params.tenantId;
  return xhr.request({
    url: `device/deviceAbility/${tenantId}/video/${deviceId}/real-time`,
    method: 'get',
  })
};